package cn.edu.lit.jsjxy.CourseExample.chapter8;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import cn.edu.lit.jsjxy.CourseExample.R;

public class Activity_EX_8_2 extends Activity
{
    static EditText etUsername;
    static EditText etPhone;
    static EditText etHomeAddr;
    static EditText etEmail;

    Cursor cursor;
    Button createBtn, openBtn, upBtn, downBtn;
    Button addBtn, updateBtn, deleteBtn, closeBtn;
    SQLiteDatabase db;
    DBConnection helper;

    public int id_this;
    Bundle savedInstanceState;
    //定义数据库名称及结构
    static String TABLE_NAME = "Users";          //数据表名
    static String ID = "_id";                    //ID
    static String USER_NAME = "user_name";       //用户名
    static String ADDRESS = "address";           //地址
    static String TELEPHONE = "telephone";       //联系电话
    static String MAIL_ADDRESS = "mail_address"; //电子邮箱

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex_8_2);

        etUsername = (EditText) findViewById(R.id.etUsername);
        etPhone = (EditText) findViewById(R.id.etPhone);
        etHomeAddr = (EditText) findViewById(R.id.etHomeAddr);
        etEmail = (EditText) findViewById(R.id.etEmail);
        createBtn = (Button) findViewById(R.id.btnCreateDatabase);
        createBtn.setOnClickListener(new ClickEvent());
        openBtn = (Button) findViewById(R.id.btnOpenDatabase);
        openBtn.setOnClickListener(new ClickEvent());
        upBtn = (Button) findViewById(R.id.btnPrev);
        upBtn.setOnClickListener(new ClickEvent());
        downBtn = (Button) findViewById(R.id.btnNext);
        downBtn.setOnClickListener(new ClickEvent());
        addBtn = (Button) findViewById(R.id.btnAddRec);
        addBtn.setOnClickListener(new ClickEvent());
        updateBtn = (Button) findViewById(R.id.btnUpdateRec);
        updateBtn.setOnClickListener(new ClickEvent());
        deleteBtn = (Button) findViewById(R.id.btnDeleteRec);
        deleteBtn.setOnClickListener(new ClickEvent());
        closeBtn = (Button) findViewById(R.id.btnCloseContacts);
        closeBtn.setOnClickListener(new ClickEvent());
    }

    class ClickEvent implements OnClickListener
    {
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.btnCreateDatabase:
                    helper = new DBConnection(Activity_EX_8_2.this);
                    SQLiteDatabase db = helper.getWritableDatabase();
                    break;
                case R.id.btnOpenDatabase:
                    db = openOrCreateDatabase("PhoneBook.db", Context.MODE_PRIVATE, null);
                    cursor = db.query("Users", null, null, null, null, null, null);
                    cursor.moveToNext();
                    upBtn.setClickable(true);
                    downBtn.setClickable(true);
                    deleteBtn.setClickable(true);
                    updateBtn.setClickable(true);
                    break;
                case R.id.btnPrev:
                    if (!cursor.isFirst()) cursor.moveToPrevious();
                    datashow();
                    break;
                case R.id.btnNext:
                    if (!cursor.isLast()) cursor.moveToNext();
                    datashow();
                    break; //
                case R.id.btnAddRec:
                    add();
                    onCreate(savedInstanceState);
                    break;
                case R.id.btnUpdateRec:
                    update();
                    onCreate(savedInstanceState);
                    break;
                case R.id.btnDeleteRec:
                    delete();
                    onCreate(savedInstanceState);
                    break;
                case R.id.btnCloseContacts:
                    cursor.close();
                    etUsername.setText("数据库已关闭");
                    etPhone.setText("数据库已关闭");
                    etHomeAddr.setText("数据库已关闭");
                    etEmail.setText("数据库已关闭");
                    upBtn.setClickable(false);
                    downBtn.setClickable(false);
                    deleteBtn.setClickable(false);
                    updateBtn.setClickable(false);
                    break;
            }
        }
    }

    /* 显示记录 */
    void datashow()
    {
        id_this = Integer.parseInt(cursor.getString(0));
        String user_name_this = cursor.getString(1);
        String telephone_this = cursor.getString(2);
        String address_this = cursor.getString(3);
        String mail_address_this = cursor.getString(4);
        etUsername.setText(user_name_this);
        etPhone.setText(telephone_this);
        etHomeAddr.setText(address_this);
        etEmail.setText(mail_address_this);
    }

    /* 添加记录 */
    void add()
    {
        ContentValues values1 = new ContentValues();
        values1.put(USER_NAME, Activity_EX_8_2.etUsername.getText().toString());
        values1.put(TELEPHONE, Activity_EX_8_2.etPhone.getText().toString());
        values1.put(ADDRESS, Activity_EX_8_2.etHomeAddr.getText().toString());
        values1.put(MAIL_ADDRESS, Activity_EX_8_2.etEmail.getText().toString());
        SQLiteDatabase db2 = helper.getWritableDatabase();
        db2.insert(TABLE_NAME, null, values1);
        db2.close();
    }

    /* 修改记录 */
    void update()
    {
        ContentValues values = new ContentValues();
        values.put(USER_NAME, Activity_EX_8_2.etUsername.getText().toString());
        values.put(TELEPHONE, Activity_EX_8_2.etPhone.getText().toString());
        values.put(ADDRESS, Activity_EX_8_2.etHomeAddr.getText().toString());
        values.put(MAIL_ADDRESS, Activity_EX_8_2.etEmail.getText().toString());
        String where1 = ID + " = " + id_this;
        SQLiteDatabase db1 = helper.getWritableDatabase();
        db1.update(TABLE_NAME, values, where1, null);
        db1.close();
    }

    /*  删除记录 */
    void delete()
    {
        String where = ID + " = " + id_this;
        db.delete(TABLE_NAME, where, null);
        db = helper.getWritableDatabase();
        db.close();
    }
}
